#include <boinc_api.h>
#include <diagnostics.h>
#include <string>
#include <iostream>


std::string resolve_filename(std::string const& filename)
{
        std::string new_filename;
        int rc = boinc_resolve_filename_s(filename.c_str(),new_filename);
        if (rc != 0)
        {
                std::cout << "Error in simona_resolve_filename, maybe file not found." << std::endl;
        }
        return new_filename;
}



int main () {
	// we init BOINC:
	boinc_init_diagnostics(BOINC_DIAG_REDIRECTSTDOUT | BOINC_DIAG_REDIRECTSTDERR |BOINC_DIAG_TRACETOSTDERR| BOINC_DIAG_HEAPCHECKENABLED);
	std::string myfilename;
	//We want to write to a file with the logical filename outfile (without BOINC this would just link to the physical file "outfile")
	myfilename = resolve_filename(std::string("outfile"));

	//on a normal computer this will write "outfile". On boinc this will give back some random string to the sandbox:
	std::cout << "hello, I will write to: " << myfilename  << std::endl;
	
	//Then we write to the resolved file as usual:
	FILE* myfile = std::fopen(myfilename.c_str(),"w");
	std::fprintf(myfile,"testout\n");

	//and we kill BOINC, this function does not return.
        boinc_finish(0);

	std::cout << "never called" << std::endl; // <- this will never be executed.
}
